package com.wtx.chapter04;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @description: 用于检测空闲的事件
 * @author: wtx
 * @createDate: 2020/5/29
 */
public class MyServerHandler extends ChannelInboundHandlerAdapter {

    // IdleStateEvent发生的回调
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent){
            IdleStateEvent event = (IdleStateEvent) evt;

            String eventType = null;
            switch (event.state()){
                case ALL_IDLE:
                    eventType = "读写空闲";
                    break;
                case WRITER_IDLE:
                    eventType = "写空闲";
                    break;
                case READER_IDLE:
                    eventType = "读空闲";
                    break;
            }
            System.out.println(ctx.channel().remoteAddress() + "超时, "+eventType);
            ctx.channel().close();
        }
    }
}
